﻿using System;
using System.Collections;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using BLL.RF.Repair;
using Model;

public partial class ProductManage_RFProductManage_Manage_JointingFailureManage_JointingFailure_ManagePack : System.Web.UI.Page
{
    private const string STEPITEM_外观检验工序 = "PackCheckStep";
    private JointingFailure jf;
    private RepairRoute rr;

    public ProductManage_RFProductManage_Manage_JointingFailureManage_JointingFailure_ManagePack()
    {
        jf = new JointingFailure();
        rr = new RepairRoute();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindJointingFailure();
        }
    }

    protected void BindJointingFailure()
    {
        this.GridView1.DataSource = jf.JointingFailure_GetbyPack();
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int failureId = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        if (failureId == 75)
        {
            string str1 = "此项为固定的合格项，不能删除，请确认！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        jf.Delete(failureId);
        BindJointingFailure();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        if (((DropDownList)e.Row.FindControl("DDLRepairPath")) != null)
        {
            DropDownList ddlps = (DropDownList)e.Row.FindControl("DDLRepairPath");
            ddlps.DataSource = rr.RepairRoute_GetbyStepItem(STEPITEM_外观检验工序);
            ddlps.DataTextField = "RepairPath";
            ddlps.DataValueField = "RepairPath";
            ddlps.DataBind();
            ddlps.SelectedValue = ((HiddenField)e.Row.FindControl("HDFRepairPath")).Value;
        }
        if (((DetailsView)e.Row.FindControl("DetailsView1")) != null)
        {
            DetailsView d = (DetailsView)e.Row.FindControl("DetailsView1");
            if (((DropDownList)d.Rows[1].Cells[1].FindControl("DDLRepairPath")) != null)
            {
                DropDownList ddlsi = (DropDownList)d.Rows[1].Cells[1].FindControl("DDLRepairPath");
                ddlsi.DataSource = rr.RepairRoute_GetbyStepItem(STEPITEM_外观检验工序);
                ddlsi.DataTextField = "RepairPath";
                ddlsi.DataValueField = "RepairPath";
                ddlsi.DataBind();
            }
        }
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindJointingFailure();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindJointingFailure();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        JointingFailureInfo failureInfo = new JointingFailureInfo();
        failureInfo.FailureID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        failureInfo.FailureName = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        failureInfo.RepairPath = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLRepairPath"))).SelectedValue;
        failureInfo.FailureType = "Pack";

        jf.Update(failureInfo);
        GridView1.EditIndex = -1;
        BindJointingFailure();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        string failureName = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        string repairPath = ((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLRepairPath"))).SelectedValue;
        if (failureName.Length > 49)
        {
            string str1 = "不良项名称长度不能大于50，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            JointingFailureInfo failureInfo = new JointingFailureInfo() { FailureID = 0, FailureName = failureName, FailureType = "Pack", RepairPath = repairPath };
            try
            {
                jf.Insert(failureInfo);
                string str1 = "添加成功！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                BindJointingFailure();
            }
            catch (Exception ex)
            {
                if (ex.Message == "The FailureName is already exist")
                {
                    string str1 = "不良项名称已存在！";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                }
            }
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindJointingFailure();
    }
}
